package com.onesatoshi.mm.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class ChatDao {

    private DBHelp dbHelp;
    private SQLiteDatabase db;

    public ChatDao(Context context) {
        dbHelp = new DBHelp(context);
    }

    public void save(Chat chat) {
        db = dbHelp.getWritableDatabase();
        String sql = "insert into t_chat(nickname, wxid, deviceid, type, content, status) values(?,?,?,?,?,?)";
        db.execSQL(sql, new String[]{chat.getNickname(), chat.getWxid(),
                chat.getDeviceid(), chat.getType(), chat.getContent(), chat.getStatus()});
        db.close();
    }

    public void update(Chat chat) {
        db = dbHelp.getWritableDatabase();
        String sql = "update t_chat set nickname = ?, wxid = ?, deviceid=?, type = ?, content = ?, status = ? where _id = ?";
        db.execSQL(sql, new Object[]{chat.getNickname(), chat.getWxid(), chat.getDeviceid(),
                chat.getType(), chat.getContent(), chat.getStatus()});
        db.close();
    }

    public void del(int id) {
        db = dbHelp.getWritableDatabase();
        String sql = "delete from t_chat where _id = ?";
        db.execSQL(sql, new Object[]{id});
        db.close();
    }

    public List<Chat> findAll() {
        List<Chat> list = new ArrayList<>();

        db = dbHelp.getReadableDatabase();
        String sql = "select _id, nickname, wxid, deviceid, type, content, status from t_chat";
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            Chat chat = new Chat();
            chat.setId(cursor.getInt(cursor.getColumnIndex("_id")));
            chat.setNickname(cursor.getString(cursor.getColumnIndex("nickname")));
            chat.setWxid(cursor.getString(cursor.getColumnIndex("wxid")));
            chat.setDeviceid(cursor.getString(cursor.getColumnIndex("deviceid")));
            chat.setType(cursor.getString(cursor.getColumnIndex("type")));
            chat.setContent(cursor.getString(cursor.getColumnIndex("content")));
            chat.setStatus(cursor.getString(cursor.getColumnIndex("status")));
            list.add(chat);
        }
        db.close();
        return list;
    }


    public List<Chat> findAllToDelete() {
        List<Chat> list = new ArrayList<>();

        db = dbHelp.getReadableDatabase();
        String sql = "select _id, nickname, wxid, deviceid, type, content, status from t_chat where status = '0'";
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            Chat chat = new Chat();
            chat.setId(cursor.getInt(cursor.getColumnIndex("_id")));
            chat.setNickname(cursor.getString(cursor.getColumnIndex("nickname")));
            chat.setWxid(cursor.getString(cursor.getColumnIndex("wxid")));
            chat.setDeviceid(cursor.getString(cursor.getColumnIndex("deviceid")));
            chat.setType(cursor.getString(cursor.getColumnIndex("type")));
            chat.setContent(cursor.getString(cursor.getColumnIndex("content")));
            chat.setStatus(cursor.getString(cursor.getColumnIndex("status")));
            list.add(chat);
        }
        db.close();
        return list;
    }

    /**
     * 统计未处理的条数
     *
     * @return
     */
    public int countUndoNum() {
        db = dbHelp.getReadableDatabase();
        String sql = "select count(1) from t_chat where status = ?";

        Cursor cursor = db.rawQuery(sql, new String[]{"0"});
        int cnt = cursor.getCount();
        db.close();

        return cnt;
    }

}
